package com.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.SessionData;
import com.server.service.IMngMenuService;
import com.server.web.SessionUtil;
/**
 * 菜单管理
 * 
 * @author xiaoxiaoping
 * @date 2016年7月22日
 */
@Controller("MngMenuController")
@RequestMapping("adminMenu")
public class MngMenuController {
	@Autowired
	private IMngMenuService mngMenuService;
	
	/*
	 * 菜单列表
	 */
	@Login
	@RequestMapping("")
	public ModelAndView menuList(HttpServletRequest request){
		List<Map<String, Object>> menuList = mngMenuService.menuList();
		ModelAndView mv = new ModelAndView("mngMenu/menuList");
		mv.addObject("menuList", menuList);
		return mv;
		
	}
	/*
	 * 添加一级菜单
	 */
	@RequestMapping("addMenuFirst")
	public ModelAndView addMenuFirst(HttpServletRequest request){
		if (request.getMethod().equalsIgnoreCase("post")){
			String name = request.getParameter("name");
			int sort = Integer.parseInt(request.getParameter("sort"));
			String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
			Boolean isSuccess = mngMenuService.addMenuFirst(name, sort, operator);
			if(isSuccess){
				return new ModelAndView("redirect:/adminMenu");
			}
			return null;
		}
		
		return new ModelAndView("mngMenu/menuAdd");
	}
	/*
	 * 添加二级菜单
	 */
	@RequestMapping("addMenuSecond")
	public ModelAndView addMenuSecond(HttpServletRequest request){
		if(request.getMethod().equalsIgnoreCase("post")){
			String name = request.getParameter("name");
			int sort = Integer.parseInt(request.getParameter("sort"));
			String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
			String function = request.getParameter("function");
			int p_id = Integer.parseInt(request.getParameter("p_id"));
			Boolean isSuccess = mngMenuService.addMenuSecond(name, function, p_id, sort, operator);
			if(isSuccess){
				return new ModelAndView("redirect:/adminMenu");
			}
		}
		List<Map<String, Object>> firstMenu = mngMenuService.getFirstMenu();
		ModelAndView mv = new ModelAndView("mngMenu/subMenuAdd");
		mv.addObject("firstMenu", firstMenu);
		return mv;
	}
	@RequestMapping("editMenu")
	public ModelAndView updateMenu(HttpServletRequest request){
		if(request.getMethod().equalsIgnoreCase("post")){
			String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
			Map<String, Object> inputMap = new HashMap<String, Object>();
			String name = request.getParameter("name");
			String id = request.getParameter("id");
			if(!StringUtils.isEmpty(request.getParameter("p_id"))){
				int p_id = Integer.parseInt(request.getParameter("p_id"));
				inputMap.put("p_id", p_id);
			}
			if(!StringUtils.isEmpty("function")){
				String function = request.getParameter("function");
				inputMap.put("function", function);
			}
			String sort = request.getParameter("sort");
			inputMap.put("sort", sort);
			inputMap.put("name", name);
			Boolean isSuccess = mngMenuService.updateMenu(inputMap, operator, "id", id);
			if(isSuccess){
				return new ModelAndView("redirect:/adminMenu");
			}else {
				System.out.println("错误");
			}
		}
		int id = Integer.parseInt(request.getParameter("id"));
		List<Map<String, Object>> firstMenuList = mngMenuService.getFirstMenu();
		Map<String, Object> menuMap = mngMenuService.getMenuById(id);
		ModelAndView mv = new ModelAndView("mngMenu/menuEdit");
		mv.addObject("firstMenuList", firstMenuList);
		mv.addObject("menuMap", menuMap);
		return mv;
	}
	@RequestMapping("deleteMenu")
	public ModelAndView deletMenu(HttpServletRequest request){
		String id = request.getParameter("id");
		Boolean isSuccess = mngMenuService.deleteMenu("id", id);
		if(isSuccess){
			return new ModelAndView("redirect:/adminMenu");
		}
		return null;
	}
}
